//
//  CoGeMoviePreload.m
//  CoGeMoviePlayer
//
//  Created by Tamas Nagy on 30/10/13.
//
//

#import "CoGeMoviePreload.h"
#import "CoGeMoviePlayerPlugIn.h"
#import <OpenGL/CGLMacro.h>

@implementation CoGeMoviePreload

@synthesize delegate;
@synthesize bank;
@synthesize slot;
@synthesize path;

-(BOOL)canHandleFileAtURL:(NSURL *)_url {
    
	NSString *fileuti = nil;
	[_url getResourceValue:&fileuti forKey:NSURLTypeIdentifierKey error:nil];
	
	if ([fileuti isEqualToString:@"com.apple.quartz-composer-composition"]) {
		
		return NO;
	}
    
    NSString *ext = [_url pathExtension];

    if ([ext isEqualToString:@"mp4"] || [ext isEqualToString:@"mov"] || [ext isEqualToString:@"avi"] || [ext isEqualToString:@"dv"]) {
		
		return YES;
	}

	return NO;
}

-(void)main {
    
    if ([self isCancelled]) {
        
        return;
    }
    
    [self performSelectorOnMainThread:@selector(preloadMovie) withObject:nil waitUntilDone:NO];
    
}

-(void)preloadMovie {
    
    NSURL *newurl = [NSURL fileURLWithPath:[self.path stringByStandardizingPath]];
    
    if ([QTMovie canInitWithFile:[self.path stringByStandardizingPath]] && [self canHandleFileAtURL:newurl]) {
        
        NSMutableDictionary* movieAttributes = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                                newurl, QTMovieURLAttribute,
                                                [NSNumber numberWithBool:NO],QTMovieLoopsAttribute,
                                                [NSNumber numberWithBool:NO],QTMovieLoopsBackAndForthAttribute,
                                                [NSNumber numberWithBool:YES], QTMovieOpenAsyncOKAttribute,
                                                [NSNumber numberWithBool:NO], QTMovieOpenForPlaybackAttribute,
                                                [NSNumber numberWithBool:YES], QTMovieEditableAttribute,
                                                //[NSNumber numberWithBool:YES], QTMovieOpenAsyncRequiredAttribute,
                                                nil];
        
        
        QTMovie *m = [[QTMovie alloc] initWithAttributes:movieAttributes error:NULL];
        
        if (m) {
            
            if (HapQTMovieHasHapTrackPlayable(m)) {
                
                
                if (![self isCancelled]) {
                    
                    [delegate setPreloadedMovie:m onBank:self.bank onSlot:self.slot];
                }
                
            } else {
                
                [m gotoBeginning];
                if (![self isCancelled]) {
                    
                    [delegate setPreloadedMovie:m onBank:self.bank onSlot:self.slot];
                }
                
                
            }
            
            
            [m release];
            
        }
        
        
        
    }

}

-(BOOL)isConcurrent {
    
    return YES;
}

-(void)dealloc {
    
    [path release];
    
    [super dealloc];
}

@end
